package grad.nxu.recruitsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import grad.nxu.recruitsystem.domain.Company;
import grad.nxu.recruitsystem.domain.Feedback;
import grad.nxu.recruitsystem.domain.Hr;
import grad.nxu.recruitsystem.service.BiographicalNotesService;
import grad.nxu.recruitsystem.service.CompanyService;
import grad.nxu.recruitsystem.service.FeedbackService;
import grad.nxu.recruitsystem.service.HrService;
import grad.nxu.recruitsystem.util.JWTUtil;
import grad.nxu.recruitsystem.vo.ApiResponse;
import grad.nxu.recruitsystem.vo.BiographicalNotesVO;
import grad.nxu.recruitsystem.vo.JobApplicationVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: recruitSystem
 * @Package_name: grad.nxu.recruitsystem.controller
 * @Date: 2023/3/13 14:03
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/hr")
public class HrController {

    @Autowired
    HrService hrService;
    @Autowired
    CompanyService companyService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    BiographicalNotesService biographicalNotesService;
    @Autowired
    FeedbackService feedbackService;

    /**
     * 获取所有招聘者信息
     *
     * @param pageSize   每页多少条
     * @param pageNumber 第几页，从1开始
     * @return 返回获取结果
     */
    @ApiOperation("获取所有招聘者信息")
    @GetMapping("/admin/all")
    public ApiResponse<List<Hr>> getAll(Integer pageSize, Integer pageNumber) {
        ApiResponse<List<Hr>> apiResponse = new ApiResponse<>();
        Page<Hr> page = new Page<>(pageNumber, pageSize);
        Page<Hr> seekerPage;
        try {
            QueryWrapper<Hr> queryWrapper = new QueryWrapper<>();
            seekerPage = hrService.page(page, queryWrapper);
            for (Hr seeker : seekerPage.getRecords()) {
                seeker.setPassword(null);
            }
            apiResponse.setCode(200).setSuccess(true).setData(seekerPage.getRecords());
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(true).setMessage("服务器错误");
        }

        return apiResponse;
    }

    /**
     * 封号接口
     *
     * @param id 招聘者id
     * @return 返回封号结果
     */
    @ApiOperation("封号接口")
    @PutMapping("/admin/lock")
    public ApiResponse<String> lockHr(Integer id) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            UpdateWrapper<Hr> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", id);
            updateWrapper.set("status", false);
            hrService.update(null, updateWrapper);
            apiResponse.setCode(200).setSuccess(true).setMessage("封禁账号成功");
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(true).setMessage("服务器错误");
        }

        return apiResponse;
    }

    /**
     * 解封接口
     *
     * @param id 招聘者id
     * @return 返回封号结果
     */
    @ApiOperation("解封接口")
    @PutMapping("/admin/unLock")
    public ApiResponse<String> unLockHr(Integer id) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            UpdateWrapper<Hr> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", id);
            updateWrapper.set("status", true);
            hrService.update(null, updateWrapper);
            apiResponse.setCode(200).setSuccess(true).setMessage("封禁账号成功");
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(true).setMessage("服务器错误");
        }

        return apiResponse;
    }

    /**
     * @Description: hr登录后添加公司信息（最多添加一个）
     * @Author: shang_kui EnglishName:Jack
     * @CreateTime: 2023/3/25 18:42
     * @param: company
     * @return: grad.nxu.recruitsystem.vo.ApiResponse<java.lang.String>
     **/
    @ApiOperation("添加公司信息")
    @PutMapping("/add")
    public ApiResponse<String> addCompany(Company company) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        String token = stringRedisTemplate.opsForValue().get("recruit:token:" + company.getEmail());
        Integer id = (Integer) JWTUtil.getPayload(token, "id");
        try {
            QueryWrapper<Hr> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            queryWrapper.eq("company_id", null);
            if (hrService.count(queryWrapper) == 0) {
                boolean b = companyService.insertCompanyInfo(company);
                if (b) {
                    UpdateWrapper<Hr> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.set("company_id", company.getId());
                    apiResponse.setCode(200).setSuccess(true).setMessage("添加公司信息成功");
                } else {
                    apiResponse.setCode(500).setSuccess(false).setMessage("添加公司信息失败");
                }
            } else {
                apiResponse.setCode(403).setSuccess(false).setMessage("仅允许添加一次公司信息");
            }
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器错误");
        }
        return apiResponse;
    }

    /**
     * @Description: 修改公司信息
     * @Author: shang_kui EnglishName:Jack
     * @CreateTime: 2023/3/25 18:41
     * @param: company
     * @return: grad.nxu.recruitsystem.vo.ApiResponse<java.lang.String>
     **/
    @ApiOperation("修改公司信息")
    @PutMapping("/update")
    public ApiResponse<String> updateCompanyInfo(Company company) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            UpdateWrapper<Company> companyUpdateWrapper = new UpdateWrapper<>();
            companyUpdateWrapper.eq("id", company.getId());
            companyUpdateWrapper.set("name", company.getName()).set("phone", company.getPhone())
                    .set("email", company.getEmail()).set("address", company.getAddress())
                    .set("boss", company.getBoss()).set("introduction", company.getIntroduction());
            companyService.update(companyUpdateWrapper);
            apiResponse.setCode(200).setSuccess(true).setMessage("公司信息修改成功");
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器错误");
        }
        return apiResponse;
    }

    /**
     * @Description: 删除公司信息
     * @Author: shang_kui EnglishName:Jack
     * @CreateTime: 2023/3/25 18:41
     * @param: id
     * @return: grad.nxu.recruitsystem.vo.ApiResponse<java.lang.String>
     **/
    @ApiOperation("删除公司信息")
    @DeleteMapping("/delete")
    public ApiResponse<String> deleteCompany(Integer id) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            companyService.removeById(id);
            apiResponse.setCode(200).setSuccess(true).setMessage("删除公司信息成功");
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器错误");
        }
        return apiResponse;
    }
    /**
     * @Description: hr查询所有投递本公司岗位的简历
     * @Author: shang_kui EnglishName:Jack
     * @CreateTime: 2023/3/26 14:31
     * @param: pageSize
     * @param: pageNumber
     * @return: grad.nxu.recruitsystem.vo.ApiResponse<java.util.List<grad.nxu.recruitsystem.vo.JobApplicationVO>>
     **/
    @ApiOperation("hr查看所有简历")
    @GetMapping("/notes/all")
    public ApiResponse<List<JobApplicationVO>> queryBiographicalNotesAll(Integer pageSize, Integer pageNumber) {
        ApiResponse<List<JobApplicationVO>> apiResponse = new ApiResponse<>();
        // 通过hr信息拿到公司id
        try {
            Integer companyId = 1;
            List<JobApplicationVO> biographicalNotesByPage = hrService.getBiographicalNotesByPage(companyId, pageSize, pageNumber);
            apiResponse.setCode(200).setSuccess(true).setMessage("查询所有简历成功").setData(biographicalNotesByPage);
        }catch (Exception e){
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器错误");
        }

        return apiResponse;
    }
    /**
     * @Description: hr查询符合年龄的简历
     * @Author: shang_kui EnglishName:Jack
     * @CreateTime: 2023/3/26 14:32
     * @param: age
     * @param: pageSize
     * @param: pageNumber
     * @return: grad.nxu.recruitsystem.vo.ApiResponse<java.util.List<grad.nxu.recruitsystem.vo.JobApplicationVO>>
     **/
    @ApiOperation("hr查看符合年龄简历")
    @GetMapping("/notes/age")
    public ApiResponse<List<JobApplicationVO>> queryBiographicalNotesByAge(Integer age,Integer pageSize, Integer pageNumber) {
        ApiResponse<List<JobApplicationVO>> apiResponse = new ApiResponse<>();
        try {
            // 通过hr信息拿到公司id
            Integer companyId = 1;
            List<JobApplicationVO> biographicalNotesByAge = hrService.getBiographicalNotesByAge(age, companyId, pageSize, pageNumber);
            apiResponse.setCode(200).setSuccess(true).setMessage("查询所有年龄小于"+age+"的简历成功").setData(biographicalNotesByAge);
        }catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器错误");
        }
        return apiResponse;
    }
    /**
     * @Description: hr查询某个性别的全部简历
     * @Author: shang_kui EnglishName:Jack
     * @CreateTime: 2023/3/26 14:33
     * @param: gender
     * @param: pageSize
     * @param: pageNumber
     * @return: grad.nxu.recruitsystem.vo.ApiResponse<java.util.List<grad.nxu.recruitsystem.vo.JobApplicationVO>>
     **/
    @ApiOperation("hr根据性别查询简历")
    @GetMapping("/notes/gender")
    public ApiResponse<List<JobApplicationVO>> queryBiographicalNotesBySex(String gender,Integer pageSize, Integer pageNumber) {
        ApiResponse<List<JobApplicationVO>> apiResponse = new ApiResponse<>();
        try {
            // 通过hr信息拿到公司id
            Integer companyId = 1;
            List<JobApplicationVO> biographicalNotesByGender = hrService.getBiographicalNotesByGender(gender, companyId, pageSize, pageNumber);
            apiResponse.setCode(200).setSuccess(true).setMessage("查询所有"+gender+"性简历成功").setData(biographicalNotesByGender);
        }catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器错误");
        }
        return apiResponse;
    }
    /**
     * @Description: 通过简历id查询简历内容
     * @Author: shang_kui EnglishName:Jack
     * @CreateTime: 2023/3/26 14:42
     * @param: biographicalId
     * @return: grad.nxu.recruitsystem.vo.ApiResponse<grad.nxu.recruitsystem.vo.BiographicalNotesVO>
     **/
    @ApiOperation("hr查看简历内容")
    @GetMapping("/notes/content")
    public ApiResponse<BiographicalNotesVO> checkOneBiographicalNote(Integer biographicalId){
        ApiResponse<BiographicalNotesVO> apiResponse = new ApiResponse<>();
        try {
            BiographicalNotesVO oneBiographicalNote = hrService.getOneBiographicalNote(biographicalId);
            apiResponse.setCode(200).setSuccess(true).setMessage("查看"+oneBiographicalNote.getName()+"的简历成功").setData(oneBiographicalNote);
        }catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器错误");
        }
        return apiResponse;
    }
    /**
     * @Description: hr提交反馈信息
     * @Author: shang_kui EnglishName:Jack
     * @CreateTime: 2023/3/27 15:04
     * @param: feedback
     * @return: grad.nxu.recruitsystem.vo.ApiResponse<grad.nxu.recruitsystem.domain.Feedback>
     **/
    @ApiOperation("hr提交反馈信息")
    @PutMapping("/feedback")
    public ApiResponse<Feedback> sendFeedback(Feedback feedback){
        ApiResponse<Feedback> apiResponse = new ApiResponse<>();
        try {
            feedbackService.save(feedback);
            apiResponse.setCode(200).setSuccess(true).setMessage("提交反馈信息成功");
        }catch (Exception e){
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器错误");
        }
        return apiResponse;
    }
}
